#include "frm_beats_aca_result.h"
#include "ui_frm_beats_aca_result.h"
#include <cstdio>
#include "frm_menu.h"
#include "frm_beats_aca.h"
#include <QDesktopWidget>
#include <QFile>
#include <QTextStream>
#include <QDate>

frm_beats_aca_result::frm_beats_aca_result(QDialog *back, int beats, QDialog *parent) :
    QDialog(parent),
    ui(new Ui::frm_beats_aca_result)
{
    back_=back;
    ui->setupUi(this);
    QFile profiledata("/sdcard/FitByYou/profile.txt");
    if(!profiledata.open(QIODevice::ReadOnly))
        qFatal("Error de lectura del fichero profile.txt");
    QTextStream in(&profiledata);
    QString sex;
    qint32 day, month, year;
    in.readLine();
    in.readLine();
    in.readLine();
    in.readLine();
    in.readLine();
    sex = in.readLine();
    year = (in.readLine()).toInt();
    month = (in.readLine()).toInt();
    day = (in.readLine()).toInt();
    profiledata.close();
    QDate *today = new QDate;
    qint32 tday, tmonth, tyear;
    tday = today->currentDate().day();
    tmonth = today->currentDate().month();
    tyear = today->currentDate().year();
    int edad = tyear - year -1;
    if(tmonth >= month and tday >= day)
        ++edad;
    int MAX=0;
    if(sex == "Man")
        MAX = 220 - edad;
    else
        if(sex == "Woman")
            MAX = 226 - edad;
    beats*=6;
    if(beats < MAX*0.6)
        ui->recom->setText("You should increase your heart rate");
    else{
        if(beats >= MAX*0.6 and beats <=MAX*0.8)
            ui->recom->setText("Your heart rate is correct");
        else{
            if(beats >= MAX*0.8){
                if(beats >= MAX)
                    ui->recom->setText("You should stop the exercise");
                else
                    ui->recom->setText("You should decrease your heart rate");
            }
        }
    }
    char number_beats[8];
    sprintf(number_beats,"%d bpm",beats);
    ui->n_beats->setText(number_beats);
    setWindowFlags( Qt:: WindowTitleHint | Qt:: CustomizeWindowHint | Qt:: WindowMinimizeButtonHint | Qt:: WindowMaximizeButtonHint);
    this->move(QApplication::desktop()->screen()->rect().center() - this->rect().center());
}

frm_beats_aca_result::~frm_beats_aca_result()
{
    delete ui;
}

void frm_beats_aca_result::on_btn_finish_clicked()
{
    this->hide();
    back_->show();
}
